import re
from rest_framework import serializers
from .models import *
from rest_framework.validators import ValidationError


# 用户序列化器
class UserSer(serializers.ModelSerializer):
    password = serializers.CharField(max_length=256, write_only=True, trim_whitespace=True)
    position = serializers.PrimaryKeyRelatedField(queryset=PositionModel.objects.all())
    position_name = serializers.SerializerMethodField(read_only=True)

    def get_position_name(self, obj):
        return obj.position.name

    class Meta:
        model = UserModel
        fields = "__all__"

    # 重写校验方法
    def validate(self, attrs):
        name = attrs.get("name")
        mobile = attrs.get("mobile")

        if not re.findall(r"[\u4e00-\u9fa5]{1,9}", name):
            raise ValidationError(f'{name}姓名由2到9为汉字组成')

        if not re.findall(r"^1[3-9]\d{9}$", mobile):
            raise ValidationError(f"{mobile}格式不正确")

        return attrs


# 部门序列化器
class DeptSer(serializers.ModelSerializer):

    class Meta:
        model = DeptModel
        fields = "__all__"

    def validate(self, attrs):
        name = attrs.get("name")
        if not re.findall(r"[\u4e00-\u9fa5]{1,9}", name):
            raise ValidationError(f'{name}名称由2到9为汉字组成')
        return attrs


# 职级序列化器
class RankSer(serializers.ModelSerializer):

    class Meta:
        model = RankModel
        fields = "__all__"

    def validate(self, attrs):
        name = attrs.get("name")
        if not re.findall(r"^[A-Za-z]{1,5}\d{1,5}$", name):
            raise ValidationError(f'{name} 职级名称由字母加数字组成！')
        return attrs


# 职位系列化器
class PositionSer(serializers.ModelSerializer):
    dept = serializers.PrimaryKeyRelatedField(queryset=DeptModel.objects.all())
    dept_name = serializers.SerializerMethodField(read_only=True)

    def get_dept_name(self, obj):
        return obj.dept.name

    rank = serializers.PrimaryKeyRelatedField(queryset=RankModel.objects.all())
    rank_name = serializers.SerializerMethodField(read_only=True)

    def get_rank_name(self, obj):
        return obj.rank.name

    class Meta:
        model = PositionModel
        fields = "__all__"

    def validate(self, attrs):
        name = attrs.get("name")

        if not re.findall(r"^[\u4e00-\u9fa5\w]{2,30}$", name):
            raise ValidationError(f'{name} 职位名称由2到30位汉字、数字、字母或下划线组成！')

        return attrs


# 职位角色中间表
class PositionRoleSer(serializers.ModelSerializer):
    position = serializers.PrimaryKeyRelatedField(queryset=PositionModel.objects.all())
    position_name = serializers.SerializerMethodField(read_only=True)

    def get_position_name(self, obj):
        return obj.position.name

    role = serializers.PrimaryKeyRelatedField(queryset=RoleModel.objects.all())
    role_name = serializers.SerializerMethodField(read_only=True)

    def get_role_name(self, obj):
        return obj.role.name

    class Meta:
        model = PositionRoleModel
        fields = "__all__"


# 角色序列化器
class RoleSer(serializers.ModelSerializer):

    class Meta:
        model = RoleModel
        fields = "__all__"

    def validate(self, attrs):
        name = attrs.get("name")

        if not re.findall(r"^[\u4e00-\u9fa5\w]{2,30}$", name):
            raise ValidationError(f'{name} 角色名称由2到30位汉字组成！')

        return attrs


# 权限序列化器
class PermissionSer(serializers.ModelSerializer):
    role = serializers.PrimaryKeyRelatedField(queryset=RoleModel.objects.all())
    role_name = serializers.SerializerMethodField(read_only=True)

    def get_role_name(self, obj):
        return obj.role.name

    class Meta:
        model = PermissionModel
        fields = "__all__"

    def validate(self, attrs):
        name = attrs.get("name")

        if not re.findall(r"^[\u4e00-\u9fa5\w]{2,30}$", name):
            raise ValidationError(f'{name} 权限名称由2到30位汉字组成！')

        return attrs















